package com.flyme.module.base.provider.api;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.common.core.utils.DateUtils;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.entity.*;
import com.flyme.module.base.provider.service.*;
import com.flyme.module.file.provider.service.SysCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 证书操作控制器
 */
@RestController
@RequestMapping("/optionCertificate")
public class ApiThirdcertifiController{
    @Autowired
    private SysCertificateService sysCertificateService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private  SysUsercertificateService sysUsercertificateService;
    @Autowired
    private SysUserexaminationService sysUserexaminationService;
    @Autowired
    private SysCourseService sysCourseService;
    @Autowired
    private SysActivityrecordService sysActivityrecordService;
    @Autowired
    private SysChangecertificateService sysChangecertificateService;
    @Autowired
    private SysStudyconfigService sysStudyconfigService;
    @Autowired
    private SysUserstudyService sysUserstudyService;
    @Autowired
    private SysContinuecourseService sysContinuecourseService;

    /**
     * 我的证书（全部）
     *
     * @param
     * @return
     */
    @RequestMapping("/meCertificate")
    public ResultBody meCertificate(String type, PagerInfo pagerInfo) {
        EntityMap entityMap = new EntityMap();
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        List<EntityMap> userctList = sysUsercertificateService.getlistCert(userAuth.getUserId(),type,pagerInfo);
        for (EntityMap map : userctList) {
            if (ObjectUtils.isNotEmpty(map.get("certifichange")) && map.get("certifichange").equals(1)) {
                //转换学分需考试
                if (ObjectUtils.isNotEmpty(map.get("isConvert")) && map.get("isConvert").equals(1)) {
                    List<EntityMap> examList = sysUserexaminationService.getlastExamination(userAuth.getUserId(), map.get("id"));
                    if (ObjectUtils.isNotEmpty(examList) && examList.size() > 0) {
                        EntityMap examInfo=new EntityMap();
                        map.put("isexam", "1");//查看考试详情;如果考试不通过，前端按钮显示重新转换考试
                        EntityMap examMap = examList.get(0);
                        examInfo.put("certExamStatus", examMap.get("examStatus"));//考试状态
                        examInfo.put("examId", examMap.get("id"));//考试id
                        examInfo.put("cerQualified", examMap.get("qualified"));//考试是否合格
                        map.put("examInfo",examInfo);
                    } else {
                        map.put("isexam", "2");//转换学分考试
                    }
                }
                //直接转换学分证明
                if (ObjectUtils.isNotEmpty(map.get("isConvert")) && map.get("isConvert").equals(0)) {
                    CriteriaQuery<EntityMap> qw_change = new CriteriaQuery();
                    qw_change.select("*");
                    qw_change.eq("userId", userAuth.getUserId());
                    qw_change.eq("sysUsercertificateId", map.get("id"));
                    qw_change.orderByDesc("createTime");
                    List<EntityMap> changeList = sysChangecertificateService.listEntityMaps(qw_change);
                    if (ObjectUtils.isNotEmpty(changeList) && changeList.size() > 0) {
                        EntityMap changeInfo=new EntityMap();
                        map.put("isApply", "1");//查看转换证明
                        EntityMap examMap = changeList.get(0);
                        changeInfo.put("auditState", examMap.get("auditState"));//转换证明状态
                        changeInfo.put("validityTime", examMap.get("validityTime"));//证明有效期
                        changeInfo.put("reason", examMap.get("reason"));//审核不通过原因
                        changeInfo.put("convertCertHtml", examMap.get("convertCertHtml"));//证明下载链接

                        map.put("changeInfo",changeInfo);
                    } else {
                        map.put("isApply", "2");//显示申请学分转换
                    }
                }
            }
            //继续教育
            if (ObjectUtils.isNotEmpty(map.get("isStudy")) && map.get("isStudy").equals(1)) {
                String awardTime = map.get("validity");//颁证日期
                Date timetimp = DateUtil.parse(awardTime,"yyyy-MM-dd");
                DateTime dateTime = DateUtil.beginOfYear(timetimp);
                Calendar startcalendar = dateTime.toCalendar();
                Calendar endcalendar = dateTime.toCalendar();
//                startcalendar.add(Calendar.YEAR, -1);
//                endcalendar.add(Calendar.YEAR, -1);
                endcalendar.add(Calendar.MONTH, 11);
                endcalendar.add(Calendar.DATE, 30);
                String startTime = DateUtils.formatDate(startcalendar.getTime(), "yyyy-MM-dd");
                String endTime = DateUtils.formatDate(endcalendar.getTime(), "yyyy-MM-dd");
                long currentTime = System.currentTimeMillis();
                if (currentTime > DateUtil.parse(startTime).getTime() && currentTime <= DateUtil.parse(endTime).getTime()) {
                    SysUserstudy sysUserstudy=sysUserstudyService.findBycertId( map.get("id").toString());
                    if(ObjectUtils.isNotEmpty(sysUserstudy)){
                        map.put("learnCourse", "1");//已购买
                    }else{
                        map.put("learnCourse", "0");//未购买
                    }
                    map.put("isLearn", "1");//继续教育
                    EntityMap study_map=new EntityMap();
                    CriteriaQuery<SysStudyconfig> qw_study = new CriteriaQuery();
                    qw_study.select("*");
                    qw_study.eq("syscertificateId", map.get("syscertifiId"));
                    List<SysStudyconfig> studyconfigList=sysStudyconfigService.list(qw_study);
                    if(studyconfigList.size()>0){
                        study_map.put("registrationFee",studyconfigList.get(0).getRegistrationFee());
                        study_map.put("period",studyconfigList.get(0).getPeriod());
                        study_map.put("targetPeriod",studyconfigList.get(0).getTagetPeriod());
                        study_map.put("offlinePeriod",studyconfigList.get(0).getOfflinePeriod());
                        study_map.put("studyStartTime",startTime);
                        study_map.put("studyEndTime",endTime);
                        Date Ttimp = DateUtil.parse(endTime,"yyyy-MM-dd");
                        DateTime dt = DateUtil.beginOfYear(Ttimp);
                        Calendar calendar = dt.toCalendar();
//                        calendar.add(Calendar.YEAR, -1);
                        calendar.add(Calendar.MONTH, 11);
                        calendar.add(Calendar.DATE, 30);
                        study_map.put("awardTime",calendar.getTime());
                        study_map.put("targetPeriod",studyconfigList.get(0).getTagetPeriod());
                        map.put("studyConfig",study_map);
                    }
                } else {
                    map.put("isLearn", "0");//继续教育超期弹框前端显示
                }
            }
        }
        if(ObjectUtils.isNotEmpty(type)&&type.equals("2")){
            EntityMap count=new EntityMap();
            CriteriaQuery<EntityMap> success_change = new CriteriaQuery();
            success_change.select("*");
            success_change.eq("userId", userAuth.getUserId());
            success_change.eq("auditState","2");//转换成功
            //申请转换证明成功
            success_change.groupBy("sysUsercertificateId");
            List<EntityMap> change_succ = sysChangecertificateService.listEntityMaps(success_change);
            // ========================== 证书认定=================================
            CriteriaQuery<EntityMap> success_change_cert = new CriteriaQuery();
            success_change_cert.select("*,sum(creditScore) as creditScore ");
            success_change_cert.eq("userId", userAuth.getUserId());
            success_change_cert.eq("changeStatus","3");//国开认定过学分
            //证书认定成功
            List<EntityMap> change_succ_cert = sysUsercertificateService.listEntityMaps(success_change_cert);
            count.put("changeSuccess",change_succ.size());
            count.put("changeCert",change_succ_cert.size());
            if(change_succ_cert.size()>0){
                count.put("creditScore",change_succ_cert.get(0).get("creditScore"));
            }else{
                count.put("creditScore",0);
            }
            entityMap.put("bankCountInfo",count);
        }
        entityMap.put("userctList",userctList);
        return ResultBody.success(entityMap);
    }


    /**
     * 我的证书（全部）
     *
     * @param
     * @return
     */
    @RequestMapping("/bankList")
    public ResultBody bankList(String type) {
        EntityMap dateMap=new EntityMap();
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        PagerInfo info=new PagerInfo();
        CriteriaQuery<EntityMap> qw = new CriteriaQuery<EntityMap>(info);
        qw.select("user.userName,user.idCard,certificate.certificateName,u.id,u.certificateNum,u.validity,u.awardTime,u.award,u.createTime as djTime, u.changeStatus as userChange," +
            "certificate.isConvert,certificate.isStudy,certificate.isChange,u.creditScore,certificate.applyType");
        qw.eq("u.userId", userAuth.getUserId());
        //学分银行
        qw.eq("certificate.isChange", "1");
        qw.createAlias("certificate", "certificate");
        qw.createAlias("user", "user");
        qw.groupBy("u.certificateId");
        if(ObjectUtils.isNotEmpty(type)){
            if(type.equals("1")){
                qw.select("user.userName,user.idCard,certificate.certificateName,u.id,u.certificateNum,u.validity,u.awardTime,u.award,u.createTime as djTime, u.changeStatus as userChange," +
                    "certificate.isConvert,certificate.isStudy,certificate.isChange,u.creditScore,certificate.applyType");
                qw.eq("u.userId", userAuth.getUserId());
                qw.eq("u.changeStatus", "");
                //学分银行
                qw.notExists("select sysUsercertificateId from  sys_changecertificate as ce where ce. sysUsercertificateId=u.id");
                qw.eq("certificate.isChange", "1");
                qw.createAlias("certificate", "certificate");
                qw.createAlias("user", "user");
            }
            if(type.equals("2")){
                qw.select("user.userName,user.idCard,certificate.certificateName,u.id,u.certificateNum,u.validity,u.awardTime,u.award,u.createTime as djTime,certificate.isConvert," +
                    "certificate.isStudy,u.changeStatus as userChange,u.creditScore,changecate.auditState,changecate.validityTime,changecate.reason,certificate.applyType");
                qw.in("changecate.auditState",1,3);//已申请与转换失败
                qw.eq("certificate.isConvert", "0");
                qw.eq("u.changeStatus", "1");
                qw.createAlias("changecate", "changecate");
            }
            if(type.equals("3")){
                qw.select("user.userName,user.idCard,certificate.certificateName,u.id,u.certificateNum,u.validity,u.awardTime,u.award, u.createTime as djTime,certificate.isConvert," +
                    "certificate.isStudy,u.changeStatus as userChange,u.creditScore,changecate.auditState,changecate.validityTime,changecate.reason,certificate.applyType");
                qw.eq("changecate.auditState",2);
                qw.eq("certificate.isConvert", "0");
                qw.eq("u.changeStatus", "2");//
                qw.createAlias("changecate", "changecate");
            }
            if(type.equals("4")){
                qw.select("user.userName,user.idCard,certificate.certificateName,u.id,u.certificateNum,u.validity,u.awardTime,u.award,u.createTime as djTime,certificate.isConvert," +
                    "certificate.isStudy,u.changeStatus as userChange,u.creditScore,changecate.auditState,changecate.validityTime,changecate.reason,certificate.applyType");
                qw.eq("changecate.auditState",2);
                qw.eq("certificate.isConvert", "0");
                qw.eq("u.changeStatus", "3");//国开已同步学分
                qw.createAlias("changecate", "changecate");
            }

        }
        List<EntityMap> userctList = sysUsercertificateService.pageList(qw).getRows();
        for (EntityMap map : userctList) {
                //考试
                if (ObjectUtils.isNotEmpty(map.get("isConvert")) && map.get("isConvert").equals(1)) {
                    List<EntityMap> examList = sysUserexaminationService.getlastExamination(userAuth.getUserId(), map.get("id"));
                    if (ObjectUtils.isNotEmpty(examList) && examList.size() > 0) {
                        EntityMap examInfo=new EntityMap();
                        map.put("isexam", "1");//查看考试详情;如果考试不通过，前端按钮显示重新转换考试
                        EntityMap examMap = examList.get(0);
                        examInfo.put("certExamStatus", examMap.get("examStatus"));//考试状态
                        examInfo.put("examId", examMap.get("id"));//考试id
                        examInfo.put("cerQualified", examMap.get("qualified"));//考试是否合格
                        map.put("examInfo",examInfo);
                    } else {
                        map.put("isexam", "2");//转换学分考试
                    }
                }
                if (ObjectUtils.isNotEmpty(map.get("isConvert")) && map.get("isConvert").equals(0)) {
                    CriteriaQuery<EntityMap> qw_change = new CriteriaQuery();
                    qw_change.select("*");
                    qw_change.eq("c.userId", userAuth.getUserId());
                    qw_change.eq("c.sysUsercertificateId", map.get("id"));
                    qw_change.orderByDesc("createTime");
                    List<EntityMap> changeList = sysChangecertificateService.listEntityMaps(qw_change);
                    if (ObjectUtils.isNotEmpty(changeList) && changeList.size() > 0) {
                            EntityMap changeInfo=new EntityMap();
                            map.put("isApply", "1");//查看转换证明
                            EntityMap examMap = changeList.get(0);
                            changeInfo.put("auditState", examMap.get("auditState"));//转换证明状态
                            changeInfo.put("validityTime", examMap.get("validityTime"));//证明有效期
                            changeInfo.put("reason", examMap.get("reason"));//审核不通过原因
                            changeInfo.put("convertCertHtml", examMap.get("convertCertHtml"));//证明下载链接
                            map.put("changeInfo",changeInfo);
                        } else {
                            map.put("isApply", "2");//显示申请转换证明
                        }

            }
        }
           dateMap.put("userctList",userctList);
            EntityMap count=new EntityMap();
            CriteriaQuery<EntityMap> success_change = new CriteriaQuery();
            success_change.select("*");
            success_change.eq("userId", userAuth.getUserId());
            success_change.eq("auditState","2");//转换成功
            //申请转换证明成功
            success_change.groupBy("sysUsercertificateId");
            List<EntityMap> change_succ = sysChangecertificateService.listEntityMaps(success_change);
            // ========================== 证书认定=================================
            CriteriaQuery<EntityMap> success_change_cert = new CriteriaQuery();
            success_change_cert.select("*,sum(creditScore) as creditScore ");
            success_change_cert.eq("userId", userAuth.getUserId());
            success_change_cert.eq("changeStatus","3");//国开认定过学分
            //证书认定成功
            List<EntityMap> change_succ_cert = sysUsercertificateService.listEntityMaps(success_change_cert);
            count.put("changeSuccess",change_succ.size());
            count.put("changeCert",change_succ_cert.size());
            if(change_succ_cert.size()>0){
                count.put("creditScore",change_succ_cert.get(0).get("creditScore"));
            }else{
                count.put("creditScore",0);
            }
            dateMap.put("bankCountInfo",count);

        return ResultBody.success(dateMap);
    }

    /**
     * 继续教育进度列表
     *
     * @param
     * @return
     */
    @RequestMapping("/keepStudy")//继续教育配置表里取数据
    public ResultBody keepStudy(String usercertificateId) {
        EntityMap entityMap = new EntityMap();
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        PagerInfo info=new PagerInfo();
        CriteriaQuery<EntityMap> qw = new CriteriaQuery<EntityMap>(info);
        qw.select("certificate.certificateName,certificate.id as syscertifiId,u.certificateNum,u.validity,u.awardTime,u.isFull,u.userperiod,u.award,u.sumperiod,studyConfig.courseIds," +
            "studyConfig.period as configperiod,studyConfig.tagetPeriod,studyConfig.offlinePeriod,certificate.applyType");
        qw.eq("u.userId", userAuth.getUserId());
        qw.eq("u.id", usercertificateId);
        qw.createAlias("studyConfig", "studyConfig");
        qw.createAlias("certificate", "certificate");
        List<EntityMap> courseList = sysUsercertificateService.pageList(qw).getRows();
        if (ObjectUtils.isNotEmpty(courseList) && courseList.size() > 0) {
            for (EntityMap c_map : courseList) {
                if (ObjectUtils.isNotEmpty(c_map.get("courseIds"))) {
                    String[] course_str=c_map.get("courseIds").toString().split(",");
                    List<EntityMap> course_List=new ArrayList<>();
                    for(String s:course_str){
                        CriteriaQuery<EntityMap> qw_course = new CriteriaQuery<EntityMap>(info);
                        qw_course.select("c.*,cc.period");
                        qw_course.createAlias("continuecourse","continuecourse");
                        qw_course.eq("c.id",s);
                        qw_course.eq("certificateId",c_map.get("syscertifiId"));
                        List<EntityMap> course_1 = sysCourseService.pageList(qw_course).getRows();
                        course_List.addAll(course_1);
                    }
                    entityMap.put("course_List", course_List);
                }
            }
        }
//        CriteriaQuery<SysUsercertificate> qw_1 = new CriteriaQuery();
//        qw_1.select("*,sum(activety.period) as activetyPeriod");
//        qw_1.eq("u.userId", userAuth.getUserId());
//        qw_1.eq("u.id", usercertificateId);
//        qw_1.createAlias("activety", "activety");
//        List<EntityMap> activetyList = sysUsercertificateService.listEntityMaps(qw_1);
        entityMap.put("courseList", courseList);
//        entityMap.put("activetyList", activetyList);
        return ResultBody.success(entityMap);
    }

    /**
     * 添加活动学时
     *
     * @param
     * @return
     */
    @PostMapping("/addActivity")
    public ResultBody addActivity(SysActivityrecord sysActivityrecord) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        sysActivityrecord.setUserId(userAuth.getUserId());
        PagerInfo pagerInfo=new PagerInfo();
        List<EntityMap> getstudyConfig=sysUsercertificateService.getstudyConfig(sysActivityrecord.getUsercertificateId(),pagerInfo);
        if(getstudyConfig.size()>0){
            sysActivityrecord.setPeriod(getstudyConfig.get(0).get("oncePeriod"));
            sysActivityrecord.setActivityType(2);//待审核
        }
        boolean a = sysActivityrecordService.save(sysActivityrecord);
        return ResultBody.success();
    }

    /**
     * 活动学时列表
     *
     * @param
     * @return
     */
    @RequestMapping("/activeyList")
    public ResultBody activeyList(String usercertificateId) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        CriteriaQuery<SysActivityrecord> qw_1 = new CriteriaQuery();
        qw_1.select("*");
        qw_1.eq("userId", userAuth.getUserId());
        qw_1.eq("usercertificateId", usercertificateId);
        List<EntityMap> activetyList = sysActivityrecordService.listEntityMaps(qw_1);
        return ResultBody.success(activetyList);
    }

    /**
     * 学习证明
     *
     * @param
     * @return
     */
    @RequestMapping("/prooRemark")
    public ResultBody prooRemark() {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        PagerInfo pagerInfo=new PagerInfo();
        CriteriaQuery<EntityMap> qw = new CriteriaQuery(pagerInfo);
        qw.select("u.sumperiod,u.isFull,certificate.certificateName,u.awardTime,u.id as sysusercertifiId,certificate.applyType");
        qw.eq("userId", userAuth.getUserId());
        qw.eq("isFull", 1);//满学时
        qw.createAlias("certificate", "certificate");
        List<EntityMap> userctList = sysUsercertificateService.pageList(qw).getRows();
        return ResultBody.success(userctList);
    }

    /**
     * 学分兑换国开课程
     *
     * @param
     * @return
     */
    @RequestMapping("/getcertifiConfig")
    public ResultBody getcertifiConfig(String usercertificateId) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        PagerInfo info=new PagerInfo();
        CriteriaQuery<EntityMap> qw = new CriteriaQuery<EntityMap>(info);
        qw.select("certificate.certificateName,certificate.discipline,certificate.disciplineScore,certificate.applyType");
        qw.eq("userId", userAuth.getUserId());
        qw.eq("u.id", usercertificateId);
        qw.createAlias("certificate", "certificate");
        List<EntityMap> userctList = sysUsercertificateService.pageList(qw).getRows();
        if (userctList.size() > 0) {
            for (EntityMap entityMap : userctList) {
                List coursemap = new ArrayList();
                if (ObjectUtils.isNotEmpty(entityMap.get("discipline")) && ObjectUtils.isNotEmpty(entityMap.get("disciplineScore"))) {
                    String[] string1 = entityMap.get("discipline").toString().split(",");
                    String[] string2 = entityMap.get("disciplineScore").toString().split(",");
                    for (int i = 0; i < string1.length; i++) {
                        EntityMap maps=new EntityMap();
                        for (int j = 0; j < string2.length; j++) {
                            maps.put("courseScore",string1[i]+","+string2[i]);
                        }
                        coursemap.add(maps);
                    }
                }
                entityMap.put("coursemap",coursemap);
            }
        }
        return ResultBody.success(userctList);
    }
    /**
     *
     *  用户证书转换记录
     * @param
     * @return
     */
    @RequestMapping("/getchangeList")
    public ResultBody getchangeList() {
        PagerInfo info=new PagerInfo();
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        CriteriaQuery<EntityMap> qw_change = new CriteriaQuery<EntityMap>(info);
        qw_change.select("c.createTime,c.auditState,syscertificate.certificateName,certificate.certificateNum");
        qw_change.eq("c.userId", userAuth.getUserId());
        qw_change.createAlias("certificate", "certificate");
        qw_change.createAlias("syscertificate", "syscertificate");
        qw_change.orderByDesc("c.createTime");
        List<EntityMap> changeList = sysChangecertificateService.pageList(qw_change).getRows();
        return ResultBody.success(changeList);
    }
    /**
     *
     *  申请学分转换初始化页面
     * @param
     * @return
     */
    @RequestMapping("/getchangeInfo")
    public ResultBody getchangeInfo(String usercertifiId) {
        EntityMap map=new EntityMap();
        PagerInfo info=new PagerInfo();
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        CriteriaQuery qw_change = new CriteriaQuery<EntityMap>(info);
        qw_change.select("users.userName,users.idCard,users.schoolNum,users.schoolTime,syscertificate.certificateName,syscertificate.award,certificate.certificateNum,c.createTime as regTime");
        qw_change.eq("c.userId", userAuth.getUserId());
        qw_change.eq("c.sysUsercertificateId",usercertifiId);
        qw_change.createAlias("certificate", "certificate");
        qw_change.createAlias("syscertificate", "syscertificate");
        qw_change.createAlias("users", "users");
        qw_change.orderByDesc("c.createTime");
        List<EntityMap> changeList = sysChangecertificateService.pageList(qw_change).getRows();
        CriteriaQuery qw = new CriteriaQuery<EntityMap>(info);
        qw.select("user.*,u.certificateNum,u.validity,u.awardTime,u.award,certificate.certificateName");
        qw.eq("u.userId", userAuth.getUserId());
        qw.eq("u.id", usercertifiId);
        qw.eq("u.changeStatus", "");
        qw.createAlias("certificate", "certificate");
        qw.createAlias("user", "user");
        List<EntityMap> user=sysUsercertificateService.pageList(qw).getRows();
        map.put("changeList",changeList);
        map.put("user",user.get(0));
        return ResultBody.success(map);
    }
    /**
     *
     *  申请学分转换
     * @param
     * @return
     */
    @RequestMapping("/addcertChange")
    public ResultBody addcertChange(Long usercertifiId) {
        //加审核中跟成功转换的校验不能重复申请
        PagerInfo info=new PagerInfo();
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        CriteriaQuery<EntityMap> qw_change = new CriteriaQuery<EntityMap>(info);
        qw_change.select("c.createTime,c.auditState,syscertificate.certificateName,certificate.certificateNum");
        qw_change.eq("c.userId", userAuth.getUserId());
        qw_change.eq("c.sysUsercertificateId", usercertifiId);
        qw_change.in("c.auditState","1","2");//已申请跟转换成功
        qw_change.createAlias("certificate", "certificate");
        qw_change.createAlias("syscertificate", "syscertificate");
        List<EntityMap> changeList = sysChangecertificateService.pageList(qw_change).getRows();
        if(changeList.size()>0){
            return ResultBody.failed("该用户已申请转换证明");
        }
        SysUser user=sysUserService.getById(userAuth.getUserId());
        SysChangecertificate sysChangecertificate=new SysChangecertificate();
        sysChangecertificate.setAuditState("1");
        sysChangecertificate.setIdCard(user.getIdCard());
        sysChangecertificate.setSysUsercertificateId(usercertifiId);
        sysChangecertificate.setUserId(userAuth.getUserId());
        boolean a= sysChangecertificateService.save(sysChangecertificate);
        if(a){
            SysUsercertificate z=sysUsercertificateService.getById(usercertifiId);
            if(ObjectUtils.isNotEmpty(z)){
                z.setChangeStatus(1);
                sysUsercertificateService.saveOrUpdate(z);
            }
        }
        return ResultBody.success(sysChangecertificate);
    }

}

